from django.shortcuts import render
import os
# Create your views here.
from django.http import JsonResponse
from django.http import HttpResponse
from threading import Thread
import json

def handle(filenames):
    os.system('python imageCaptioning/tool/main.py ' +str(filenames))

def ImageCaptionHandle(request):
    if request.method == 'POST':
        print(request.body)
        #path = request.body
        path = request.body.decode('utf-8')
    
        path = str(path)
        #param1 = data['param1']
        data = {}

        pathArr = path.split(",")
        if os.path.exists("E:\\nginx-1.8.0\\html\\temp\static\\result\\data.json"):
            with open("E:\\nginx-1.8.0\\html\\temp\static\\result\\data.json", "r") as infile:
                data = json.load(infile)

        if data is None:
            data = {}

        if "data" not in data:
            data["data"] = {}

        for i in pathArr:
            fileName = os.path.basename(i)
            fileData = {
            "fileName": fileName,
            "filePath": i,
            "state": "正在处理",
            "result": ""
            }
            data["data"][fileName] = fileData
        with open("E:\\nginx-1.8.0\\html\\temp\static\\result\\data.json", "w") as outfile:
            json.dump(data, outfile)
        #print(param1)
        #os.system('python imageCaptioning/tool/test.py '+str(filename))
        #print("开始运行")
        # os.system('python imageCaptioning/tool/main.py')
        #print("运行完毕")
        # fileNames=['E:\\codeserver\\imageCaptioning\\imageCaptioning\\tool\\1.jpg','E:\\codeserver\\imageCaptioning\\imageCaptioning\\tool\\1.jpg']
       
        t=Thread(target=handle,args=(path,))
        t.start()
        
        # data = {my_param}
        # print(my_param)
        return HttpResponse("请求成功")
    return HttpResponse("不是POST请求")